﻿<script src="@Url.Content("~/Scripts/jquery.printElement.min.js")" type="text/javascript"></script>
<style type="text/css">
    #outrepository { width: 649px; border:0px solid; margin: auto; font-size: 12px; }
    #outrepository input { border: 0; text-align: right; width: 150px; }
    #orderitemtable { border-top: solid 1px rgb(170,170,170); border-left: solid 1px rgb(170,170,170); }
    #orderitemtable th { width: 80px; }
    #orderitemtable th, #orderitemtable td { padding:3px; border-bottom: solid 1px rgb(170,170,170); border-right: solid 1px rgb(170,170,170); }
    #orderitemtable input { border: 0px; width: 80px; }
</style>
<script type="text/javascript">
    function FloatMul(arg1, arg2) {
        var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
        try { m += s1.split(".")[1].length } catch (e) { }
        try { m += s2.split(".")[1].length } catch (e) { }
        return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
    }
    $(function () {
        $("button").button();
        $("#print").click(function () {
            $("#siteContain").css("display", "none");
            $("body").append($("#outrepository"));
            print();
            $(document).one("click", function () {
                $("#siteContain").css("display", "");
                $("#outprint").after($("#outrepository"));
            });
            //$("#outrepository").printElement();
        });

        $("#save").click(function () {
            var data = {};
            $.each($("#orderitemtable tr"), function (index, value) {
                if (index == 0) return;
                var names = [
                "ProductValueObject.Number",
                "ProductValueObject.Name",
                "ProductValueObject.Spec",
                "ProductValueObject.Model",
                "ProductValueObject.Unit",
                "SingleCount",
                "ShouldCount",
                "RealCount",
                "Date",
                "Number",
                "Remark"
                ]
                $.each($(this).find("td"), function (i, val) {
                    var n = "outDepositories[" + (index - 1) + "]." + names[i];
                    if (i == 10) {
                        data[n] = "生产批号：" + $("#producenumber").val() + " " + $("#configuration").val() + " 数量：" + $("#plancount").val();
                    } else if ($(this).find("input").length == 0) {
                        data[n] = $(this).html();
                    } else {
                        data[n] = $(this).find("input").eq(0).val();
                    }
                });
            });

            $.post("/Configuration/Out", data, function (value) {
                if (value.success) {
                    Alert("恭喜", "出库成功！");
                } else {
                    Alert("错误", value.message);
                }
            });
        });
        $("#configuration").click(function () {
            $(this).autocomplete("search", "");
        });
        $("#plancount").keyup(function () {
            $.each($("#orderitemtable tr"), function (index, value) {
                if (index == 0) return;
                $(this).find("td").eq(6).html(FloatMul($(this).find("td").eq(5).html(), $("#plancount").val()));
            });
        });
        $("#configuration").autocomplete({
            source: "/Configuration/GetConfigurations",
            minLength: 0,
            select: function (event, ui) {
                $.getJSON("/Configuration/Items?id=" + ui.item.id, function (data) {
                    //清表数据
                    $("#orderitemtable td").parent().remove();

                    var d = new Date();
                    var m = d.getMonth() + 1;
                    var date = d.getFullYear() + '-' + m + '-' + d.getDate()
                    var items = [];
                    $.each(data, function (key, val) {
                        var item = "<tr><td>" + val[0] + "</td>" +
                            "<td>" + val[1] + "</td>" +
                            "<td>" + val[2] + "</td>" +
                            "<td>" + val[3] + "</td>" +
                            "<td>" + val[4] + "</td>" +
                            "<td style='text-align: right'>" + val[5] + "</td>" +
                            '<td style="text-align: right">' + FloatMul(val[5], $("#plancount").val()) + '</td>' +
                            '<td><input type="text" value="0" /></td>' +
                            '<td><input type="text" class="date" value=' + date + ' /></td>' +
                            '<td><input type="text" class="number" /></td><td></td>';
                        items.push(item);
                    });
                    $("#orderitemtable").append(items.join(''));
                    $('.date').datepicker();
                    $('.number').click(function () {
                        var number = $(this).parent().parent().find("td").eq(0).html();
                        $(this).autocomplete({
                            source: "/InDepository/InDepositorys?number=" + number,
                            minLength: 0
                        });
                        $(this).autocomplete("search", "");
                    });
                })
            }
        });

    })
</script>
<div id="outprint"></div>
<div id="outrepository">
    <h1 style="text-align:center"><input id="configuration" type="text" style="font-size: 1em; width: 130px;" />产品领料（出库）单</h1><br />
    <div style="margin:10px">
        <label>生产批号：</label><input type="text" style="text-align: left; margin-right:100px;" id="producenumber" /><label>计划数量：</label><input id="plancount" type="text" value="0" style="width: 60px;" /><label>台</label>
    </div>
    <table id="orderitemtable" cellspacing="0" cellpadding="0" border="0" class="ui-jqgrid-btable">
        <tr>
            <th class="ui-state-default ui-th-column ui-th-ltr" style="width: 100px">物料编号</th>
            <th class="ui-state-default ui-th-column ui-th-ltr" style="width: 140px">物料名称</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">规格</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">型号</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">单位</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">单台用量</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">应发数量</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">实发数量</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">出库日期</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">批次号</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">备注</th>
        </tr>
    </table>
    <div style="margin:10px">
        <label>领料人：</label><input type="text" style="text-align: left; margin-right:100px;" /><label>仓库：</label><input id="depository" type="text" style="text-align: left;" />
    </div>
</div>
<div style=" position:absolute; top: 60px; right: 200px"><button id="save">出库</button><button id="print">打印</button></div>